package com.yonyou.train.exception;

import com.yonyou.train.Message;
import com.yonyou.train.ResultJson;
import com.yonyou.train.StatusCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    /*
    处理登录异常的方法
     */
    @ExceptionHandler(value = LoginException.class)
    public Message loginExceptionHandler(Exception exception) {

        if ("密码错误".equals(exception.getMessage())) {
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        } else {
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        }
    }

    /*
    处理登出异常
     */
    @ExceptionHandler(value = LoginOutException.class)
    public Message LoginOutExceptionHandler(Exception exception) {
        return ResultJson.error(StatusCode.ERROR, exception.getMessage());
    }

    /*
    处理更新密码异常
     */
    @ExceptionHandler(value = UpdatePwdException.class)
    public Message UpdatePwdExceptionHandler(Exception exception) {
        if (exception.getMessage().equals("用户不存在")) {
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        } else if (exception.getMessage().equals("密码不正确")) {
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        } else if (exception.getMessage().equals("密码重复")) {
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        } else {
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        }

    }

    /*
    * 处理代人报名权限异常
    * */
    @ExceptionHandler(value = PreviewServiceException.class)
    public Message PeviewServiceException(Exception exception){
        if(exception.getMessage().equals("对不起，您没有权限")) {
            return ResultJson.error(StatusCode.ERROR,exception.getMessage());
        }else if(exception.getMessage().equals("报名课程不能为空")){
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        }else if(exception.getMessage().equals("不能重复报名")){
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        }else if(exception.getMessage().equals("当前时间不能签到")){
            return  ResultJson.error(StatusCode.ERROR, exception.getMessage());
        }
        else{
            return ResultJson.error(StatusCode.ERROR, exception.getMessage());
        }

    }
    /*
    处理发布课程异常
     */
    @ExceptionHandler(value = ReleaseCourseException.class)
    public Map ReleaseCourseException(Exception e){
        return ResultJson.err(e.getMessage());
    }
    /*
   处理查看课程详情
    */
    @ExceptionHandler(value = CourseException.class)
    public Map CourseException(Exception e){
        return ResultJson.err(e.getMessage());
    }
}
